package com.example.demo.util;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.CharsetUtil;
import com.example.demo.pojo.Towers;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Tools {

    public static List<String> sortAsNumbers(Collection<String> collection) {
        return collection
                .stream()
                .map(Integer::valueOf)
                .sorted()
                .map(String::valueOf)
                .collect(Collectors.toList());
    }


    public static File getResources(String classpath) throws IOException {
        return new ClassPathResource(classpath).getFile();
    }

    public static void errorMsg(Map<String, Object> data, String msg) {
        data.put("code", 1002);
        data.put("msg", msg);
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        CsvReader csvReader = CsvUtil.getReader();
        List<Towers> datas = csvReader.read(ResourceUtil.getReader("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_272f4y3kjtp922\\FileStorage\\File\\2023-01\\csv\\csv\\dl07201111.001\\dl_tower.csv", CharsetUtil.CHARSET_GBK), Towers.class);
        for (int i = 0; i < datas.size(); i++) {
//            if (datas.get(i).getLine().equals(datas.get(i + 1).getLine())) {
//            }
            map.put(datas.get(i).getCity() + "-" + datas.get(i).getLine() + "-" + datas.get(i).getPressure(), datas.get(i).getTowers());
        }
        System.out.println(map);
    }


    /**
     * 从字符串得到数字
     *
     * @param s
     * @return
     */
    public static Integer getNumeric(String s) {
        return Integer.valueOf(Pattern.compile("[^0-9]").matcher(s).replaceAll("").trim());
    }


    public static Double getNumericD(String s) {
        Matcher matcher = Pattern.compile("\\d+\\.+\\d+").matcher(s);
        double v = 0d;
        if (matcher.find() == true) {
            v = Double.parseDouble(matcher.group());
        }
        return v;
    }

}
